//
//  ZYModifyProblemVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/31.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYModifyProblemVC.h"
#import "ZYViewUtils.h"
#import "ZYModifyUserInfoViewModel.h"
#import "ZYProblem.h"

@interface ZYModifyProblemVC ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong) UIPickerView *problemPickerView;
@property(nonatomic,strong) UITextField *problemTextField;
@property(nonatomic,strong) UITextField *answerTextField;
@property(nonatomic,strong) UIButton *confirmBtn;
@property(nonatomic,strong) NSArray *problemArray;

/**
 当前选中问题
 */
@property(nonatomic,strong) ZYProblem *problem;

/**
 用户信息修改
 */
@property(nonatomic,strong) ZYModifyUserInfoViewModel *viewModel;
@end

@implementation ZYModifyProblemVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self setupTap];
    [self bindView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_MODIFY_PROBLEM];
    [self.viewModel.problemDataCommand execute:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - init
-(void)bindView{
    @weakify(self);
    [[[self.viewModel.problemDataCommand executionSignals] switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        self.problemArray = x;
        [self.problemPickerView reloadAllComponents];
        if([self.problemArray count] > 0){
            self.problem = [self.problemArray objectAtIndex:0];
            ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
            NSString *title = @"";
            if([[language userLanguage] hasPrefix:@"en"]){
                title = self.problem.englishTitle;
            }else{
                title = self.problem.chineseTitle;
            }
            self.problemTextField.text = title;
        }
    }];
}

-(void)setupView{
    CGFloat left = 15;
    CGFloat height = 45;
    CGFloat padding = 25;
    UIPickerView *problemPickerView = [[UIPickerView alloc] init];
    //    problemPickerView.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    problemPickerView.backgroundColor = [UIColor whiteColor];
    problemPickerView.delegate = self;
    problemPickerView.dataSource = self;
    //    problemPickerView.hidden = YES;
    //    [self.view addSubview:problemPickerView];
    
    UITextField *problemTextField = [[UITextField alloc] init];
    problemTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    [self.view addSubview:problemTextField];
    problemTextField.centerX = self.view.centerX;
    problemTextField.top = self.navigationView.bottom + 20;
    [self setupTextField:problemTextField];
    //    problemTextField.delegate = self;
    problemTextField.inputView = problemPickerView;
    problemTextField.text = self.problemArray[0];
    [self setRightImageView:problemTextField imageName:@"ic_me_info_select"];
    
    UITextField *answerTextField = [[UITextField alloc] init];
    answerTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    [self.view addSubview:answerTextField];
    answerTextField.centerX = self.view.centerX;
    answerTextField.top = problemTextField.bottom + padding;
    [self setupTextField:answerTextField];
    answerTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIButton *confirmBtn = [ZYViewUtils setupConfirmBtn];
    [self.view addSubview:confirmBtn];
    confirmBtn.centerX = self.view.centerX;
    confirmBtn.top = answerTextField.bottom + 50;
    [confirmBtn addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    
    _confirmBtn = confirmBtn;
    _answerTextField = answerTextField;
    _problemTextField = problemTextField;
    _problemPickerView = problemPickerView;
    [self setupTitle];
    //    [_problemPickerView reloadAllComponents];
}

-(void)setupTextField:(UITextField *)textFiled{
    textFiled.layer.borderWidth = 1;
    textFiled.layer.borderColor = RGBA(235, 235, 235, 1).CGColor;
    textFiled.layer.cornerRadius = 5;
    textFiled.layer.masksToBounds = YES;
    [self setLeftView:textFiled];
}

/**
 初始化标题
 */
-(void)setupTitle{
    NSString *answerPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_MODIFY_ANSWER_PLACEHOLDER];
    NSString *confirmBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE];
    [_confirmBtn setTitle:confirmBtnTitle forState:UIControlStateNormal];
    _answerTextField.placeholder = answerPlaceholder;
}

/**
 设置左视图
 @param textField texField
 */
-(void)setLeftView:(UITextField *)textField{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, textField.height)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}

/**
 设置右视图
 @param textField texField
 @param imageName 显示的图片
 */
-(void)setRightImageView:(UITextField *)textField imageName:(NSString *)imageName{
    UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    rightImageView.frame = CGRectMake(0, 0, 16, 10);
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W * 0.08, ZYSCREEN_W * 0.26)];
    [rightView addSubview:rightImageView];
    rightImageView.centerY = rightView.centerY;
    textField.rightViewMode = UITextFieldViewModeAlways;
    textField.rightView = rightView;
}


#pragma mark -Event
-(void)doConfirm:(id)button{
    [self dismissKeyboard];
    self.viewModel.answer = _answerTextField.text;
    self.viewModel.problemId = _problem.code;
    @weakify(self);
    [[self.viewModel.problemModifyCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_MODIFY_PROBLEM_SUCCESS]];
        [self.navigationController popViewControllerAnimated:YES];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    //监听状态
    [[self.viewModel.problemModifyCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
}

//#pragma mark - textField delegate
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//    return NO;
//}

#pragma mark UIPickerViewDelegate DataSource
//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//返回指定列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [self.problemArray count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    ZYProblem *problem = self.problemArray[row];
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    if([[language userLanguage] hasPrefix:@"en"]){
        return problem.englishTitle;
    }else{
        return problem.chineseTitle;
    }
}


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [_problemTextField resignFirstResponder];
    _problem = self.problemArray[row];
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    NSString *title = @"";
    if([[language userLanguage] hasPrefix:@"en"]){
        title = _problem.englishTitle;
    }else{
        title = _problem.chineseTitle;
    }
    _problemTextField.text = title;
}

#pragma mark -Getter
-(ZYModifyUserInfoViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYModifyUserInfoViewModel alloc] init];
    }
    return _viewModel;
}

@end
